package gnss

import (
	"dgo/goutils/utils"
	"strings"
)

func YObsEpochEncodRtcmMsm4V0(yobs *GnssYObsEpoch, rtcmTempEpoch *RtcmRawObsEpoch, vars *EncodeVars, outBuilder *utils.BytesBuilder, onObs func(obs *RtcmRawMsmObs) bool, onAfterEncode func(msgid uint16, buf []byte)) {
	if rtcmTempEpoch == nil {
		rtcmTempEpoch = NewRtcmRawObsEpoch()
	} else {
		rtcmTempEpoch.Reset()
	}

	rtcmTempEpoch.CopyFromYObsEpoch(yobs, vars.TransVars)
	rtcmTempEpoch.TOW = yobs.TOW

	// 先处理掉不要的数据
	rtcmTempEpoch.RangeObs(func(obs *RtcmRawMsmObs) {
		if len(vars.NaviSysFilter) > 0 && !strings.Contains(vars.NaviSysFilter, GetNavSysCode(obs.Header.NaviSys)) {
			rtcmTempEpoch.DelNaviSys(obs.Header.NaviSys)
			return
		}

		if onObs != nil && !onObs(obs) {
			rtcmTempEpoch.DelNaviSys(obs.Header.NaviSys)
		}
	})

	naviSys := SYS_UNKOWN
	obs := rtcmTempEpoch.NextObs(naviSys)

	for obs != nil {

		next := rtcmTempEpoch.NextObs(obs.Header.NaviSys)
		isEndEpoch := next == nil

		if obs.Header.NaviSys == SYS_BD {
			if !vars.BD3_enable {
				obs.DelSats(17, 0)
			}

			if len(vars.BDS_DelPrns) > 0 {
				obs.DelSatsEx(vars.BDS_DelPrns...)
			}

			EncodeMsm4FromObs(obs, vars.BDS_SigWl, nil, func() *RtcmRawMsm4 {
				msm4 := NewRtcmRawMsm4()
				obs.Header.WriteInfo2MsmHeader(&msm4.Header)
				msm4.Header.EpochEndFlag = isEndEpoch
				msm4.Header.MsgID = GetMsm4MsgIDByNaviSys(msm4.Header.NaviSys)
				msm4.Header.SetTOW(rtcmTempEpoch.TOW)
				msm4.Header.ClkSteering = 1
				msm4.Header.SmoothingInterval = 1
				//msm4.MakeMsmHeaderStep0(obs)
				return msm4
			}, func(msm *RtcmRawMsm4) {
				if msm.Header.CellN == 0 {
					if msm.Header.EpochEndFlag {
						vars.outputMessage("编码到最后一个包没有,发现没有观测值")
					}
				} else {
					startIdx := outBuilder.Len()
					msm.EncodeMsm(outBuilder, startIdx)
					if onAfterEncode != nil {
						onAfterEncode(msm.Header.MsgID, outBuilder.Buff(startIdx, 0))
					}
				}
			})
		} else if obs.Header.NaviSys == SYS_GPS {
			EncodeMsm4FromObs(obs, vars.GPS_SigWl, nil, func() *RtcmRawMsm4 {
				msm4 := NewRtcmRawMsm4()
				obs.Header.WriteInfo2MsmHeader(&msm4.Header)
				msm4.Header.EpochEndFlag = isEndEpoch
				msm4.Header.MsgID = GetMsm4MsgIDByNaviSys(msm4.Header.NaviSys)
				msm4.Header.SetTOW(rtcmTempEpoch.TOW)
				msm4.Header.ClkSteering = 1
				msm4.Header.SmoothingInterval = 1
				//msm4.MakeMsmHeaderStep0(obs)
				return msm4
			}, func(msm *RtcmRawMsm4) {
				if msm.Header.CellN == 0 {
					if msm.Header.EpochEndFlag {
						vars.outputMessage("编码到最后一个包没有,发现没有观测值")
					}
				} else {
					startIdx := outBuilder.Len()
					msm.EncodeMsm(outBuilder, startIdx)
					if onAfterEncode != nil {
						onAfterEncode(msm.Header.MsgID, outBuilder.Buff(startIdx, 0))
					}
				}
			})

		} else if obs.Header.NaviSys == SYS_GLO { // GLO
			EncodeMsm4FromObs(obs, vars.GLO_SigWl, vars.GLO_SigFixed, func() *RtcmRawMsm4 {
				msm4 := NewRtcmRawMsm4()
				obs.Header.WriteInfo2MsmHeader(&msm4.Header)
				msm4.Header.EpochEndFlag = isEndEpoch
				msm4.Header.MsgID = GetMsm4MsgIDByNaviSys(msm4.Header.NaviSys)
				msm4.Header.SetTOW(rtcmTempEpoch.TOW)
				msm4.Header.ClkSteering = 1
				msm4.Header.SmoothingInterval = 1
				//msm4.MakeMsmHeaderStep0(obs)
				return msm4
			}, func(msm *RtcmRawMsm4) {
				if msm.Header.CellN == 0 {
					if msm.Header.EpochEndFlag {
						vars.outputMessage("编码到最后一个包没有,发现没有观测值")
					}
				} else {
					startIdx := outBuilder.Len()
					msm.EncodeMsm(outBuilder, startIdx)
					if onAfterEncode != nil {
						onAfterEncode(msm.Header.MsgID, outBuilder.Buff(startIdx, 0))
					}
				}
			})

		} else {
			var sigs []byte
			if obs.Header.NaviSys == SYS_GAL {
				sigs = vars.GAL_SigWl
			} else if obs.Header.NaviSys == SYS_QZS {
				sigs = vars.QZS_SigWl
			}

			EncodeMsm4FromObs(obs, sigs, nil, func() *RtcmRawMsm4 {
				msm4 := NewRtcmRawMsm4()
				obs.Header.WriteInfo2MsmHeader(&msm4.Header)
				msm4.Header.EpochEndFlag = isEndEpoch
				msm4.Header.MsgID = GetMsm4MsgIDByNaviSys(msm4.Header.NaviSys)
				msm4.Header.SetTOW(rtcmTempEpoch.TOW)
				msm4.Header.ClkSteering = 1
				msm4.Header.SmoothingInterval = 1
				//msm4.MakeMsmHeaderStep0(obs)
				return msm4
			}, func(msm *RtcmRawMsm4) {
				if msm.Header.CellN == 0 {
					if msm.Header.EpochEndFlag {
						vars.outputMessage("编码到最后一个包没有,发现没有观测值")
					}
				} else {
					startIdx := outBuilder.Len()
					msm.EncodeMsm(outBuilder, startIdx)
					if onAfterEncode != nil {
						onAfterEncode(msm.Header.MsgID, outBuilder.Buff(startIdx, 0))
					}
				}
			})
		}
		obs = next
	}
}

func YObsEpochEncodRtcmMsm4(yobs *GnssYObsEpoch, vars *EncodeVars, onObs func(obs *RtcmRawMsmObs) bool, onRtcmBuf func(msgid uint16, buf []byte)) {
	YObsEpochEncodRtcmMsm4V0(yobs, nil, vars, &vars.Builder, onObs, func(msgid uint16, buf []byte) {
		if onRtcmBuf != nil {
			onRtcmBuf(msgid, buf)
		}
		vars.ResetBuilder()
	})

}
